/****************************************************************************
**
** Copyright (C) 2013 Ernesto Barragan <ernestus at gmail dot com>
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
****************************************************************************/
#include "serialportmanager.h"
#include <QSettings>

SerialPortmanager::SerialPortmanager( QSerialPort& serialPort ) :
    m_serialPort( serialPort )
{
    QSettings settings ;
}

void SerialPortmanager::openSerialPort()
{
#if 0
    QSettings settings ;
    //SerialSettingsDialog::Settings p = m_settingsDialog->settings();
    QString name = settings.value("serial_port/name") ;

    /*
     @fixme take care of default port names and inexistent ones as well
    */
    m_serialPort.setPortName( tmpString );
    if (m_serialPort.open(QIODevice::ReadWrite))
    {
        if (m_serialPort.setBaudRate(p.baudRate)
                && m_serialPort->setDataBits(p.dataBits)
                && m_serialPort->setParity(p.parity)
                && m_serialPort->setStopBits(p.stopBits)
                && m_serialPort->setFlowControl(p.flowControl))
        {

            m_console->setEnabled(true);
            m_console->setLocalEchoEnabled(p.localEchoEnabled);
            ui->actionConnect->setEnabled(false);
            ui->actionDisconnect->setEnabled(true);
            ui->actionConfigure->setEnabled(false);
            ui->statusBar->showMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
                                       .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
                                       .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
            m_serialPortGraphicIndicator->setPixmap(QPixmap(QString::fromUtf8("://images/greenStone24.png")));

        }
        else
        {
            m_serialPort->close();
            QMessageBox::critical(this, tr("Error"), m_serialPort->errorString());
            m_serialPortGraphicIndicator->setPixmap(QPixmap(QString::fromUtf8("://images/redStone24.png")));
            ui->statusBar->showMessage(tr("Open error"));
        }
    }
    else
    {
        QMessageBox::critical(this, tr("Error"), m_serialPort->errorString());
        ui->statusBar->showMessage(tr("Configure error"));
        m_serialPortGraphicIndicator->setPixmap(QPixmap(QString::fromUtf8("://images/redStone24.png")));
    }
    m_serialPortTextIndicator->setText( m_serialPort->portName() );
#endif
}
